<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/23
 * Time: 17:12
 */

namespace app\notification\behavior;

/**
 * 分销商行为拓展
 * Class UserDistributor
 * @package app\notification\behavior
 */
class UserDistributor
{
    public function run(&$params) {}

    public function ApplyAfter(&$params) {}

    /**
     * 分销商审核
     * @param $params
     */
    public function ReviewAfter(&$params) {

        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');

        if($params['status'] == 1) {
            $notification = $lang['分销商审批成功'];
        } else {
            $notification = $lang['分销商审批失败'];
        }

        $replace = [
            '{{客户姓名}}' => $params->user->nickname,
            '{{业务类型}}' => '分销商',
            '{{业务状态}}' => $params['status_name'],
            '{{业务内容}}' => '您提交的分销商申请因 <'. $params['manage_remark'] .'> 经审批结果为：'.$params['status_name'],
        ];


        if ($notification['sms'] && $params->user->mobile) {
            $mobile = $params->user->mobile;
            $template_id = $notification['sms']['template_id'];
            $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
            model('admin/dysms', 'service')->send($mobile, $template_vars, $template_id);
        }

//        if($notification['template_message'] && $params->user->getAttr('wechat_fans')) {
//            $fans = $params->user->getAttr('wechat_fans');
//            $mini_program = config('wechat.mini_program');
//            $data = [
//                'touser' => $fans->openid,
//                'miniprogram' => [
//                    'appid' => $mini_program['app_id'],
//                    'pagepath' => '/pages/index/index',
//                ],
//                'template_id' => $notification['template_message']['template_id'],
//                'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//            ];
//            queue('app\notification\command\TemplateMessage', $data, 0);
//        }


        return;
    }
}
